//
//  SearchViewController.m
//  TodayFirstNews
//
//  Created by 向祖华 on 16/3/5.
//  Copyright © 2016年 向祖华. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchVCCollectionVCell.h"
#import "SearchVCollectionVHeader.h"

@interface SearchViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UISearchBarDelegate,UIWebViewDelegate>
@property(nonatomic,strong)NSArray * titleArr;//存储标题
@property(nonatomic,strong)NSArray * imageArr;//存储图片名车
@property(nonatomic,strong)UIViewController*searchResultVC;
@property(nonatomic,assign)CGRect webVFram;//存储web应该显示时的fram
@property(nonatomic,strong)NSTimer * time;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[[[ self.searchBar.subviews objectAtIndex : 0 ] subviews ] objectAtIndex : 0 ] removeFromSuperview ];
    self.searchBar.layer.cornerRadius = 15;
    self.searchBar.layer.borderWidth = 0.5;
    self.searchBar.layer.borderColor = [UIColor blackColor].CGColor;
    
    //设置代理
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    //注册
    [self.collectionV registerNib:[UINib nibWithNibName:@"SearchVCCollectionVCell" bundle:nil] forCellWithReuseIdentifier:@"SearchVCCollectionVCell"];
    
    [self.collectionV registerNib:[UINib nibWithNibName:@"SearchVCollectionVHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchVCollectionVHeader"];
    
    //初始化数组
    self.titleArr = @[@"文章",@"频道",@"头条号",@"视频",@"话题"];
    self.imageArr = @[@"1",@"2",@"3",@"4",@"5"];
    
    //设置searchbar的属性
    //添加辅助视图
    UIView * accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    accessoryView.backgroundColor = [UIColor redColor];
    [self.searchBar setInputAccessoryView:accessoryView];
    
    //添加记录历史
    //设置代理
    self.searchBar.delegate = self;
    //添加搜索后显示结果的控制器
    self.webVFram = CGRectMake(0, 64,kScreenWidth, kScreenHeight-50);
    
}

#pragma mark - 实现代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SearchVCCollectionVCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchVCCollectionVCell" forIndexPath:indexPath];
    cell.image.image = [UIImage imageNamed:self.imageArr[indexPath.row]];
    cell.lable.text = self.titleArr[indexPath.row];
    return cell;
    
    
}
//返回头视图
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    SearchVCollectionVHeader*headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchVCollectionVHeader" forIndexPath:indexPath];
    headerView.lable.text = @"搜索感兴趣的内容";
    UIView*view = [[UIView alloc] initWithFrame:CGRectMake(0, 59.5, kScreenWidth-120, 0.5)];
    view.backgroundColor = [UIColor grayColor];
    [headerView addSubview:view];
    return headerView;
}
- (IBAction)backButton:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


//回收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

//搜索按钮点击事件
- (IBAction)searchButtonAction:(UIButton *)sender {
    // 结束编辑回收键盘
    [self.searchBar endEditing:YES];
    // 切换界面
    NSString * urlStr = @"http://news.16888.com/a/2014/0404/357802.html";
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [self.webView loadRequest:request];
    //设置webView代理
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    
}
#pragma mark - webView的代理方法
//加载完成的时候执行改变webView的fram
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.webView.frame = self.webVFram;
}

#pragma mark - searchBar的代理方法
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    NSLog(@"%@结束编辑",searchBar.text);
    return YES;
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"开始编辑");
    return YES;
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"%@text开始编辑",searchBar.text);
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    NSLog(@"%@text结束编辑",searchBar.text);
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    NSLog(@"text改变%@",searchText);
    [self.searchButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    self.searchButton.enabled = YES;
}


#pragma mark - 懒加载
-(NSArray *)titleArr{
    if (!_titleArr) {
        _titleArr = [[NSArray alloc] init];
    }
    return _titleArr;
}
-(NSArray *)imageArr{
    if (!_imageArr) {
        _imageArr = [[NSArray alloc] init];
    }
    return _imageArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
